// Fill out your copyright notice in the Description page of Project Settings.

#include "TerrainManager.h"
#include "WinstonLogger.h"

ATerrainManager::ATerrainManager()
{
    // 设置默认值
    CurrentTerrainSeed = 12345;
    TerrainSize = 1000.0f;
    
    // 设置Actor属性
    PrimaryActorTick.bCanEverTick = true;
    
    // 初始化地形组件
    InitializeTerrainComponent();
    
    // 记录管理器初始化
    FWinstonLogger::Get().Debug(TEXT("TerrainManager"), TEXT("Manager initialized with default settings"));
}

void ATerrainManager::BeginPlay()
{
    Super::BeginPlay();
    
    // 生成初始地形
    GenerateNewTerrain(CurrentTerrainSeed);
    
    // 应用地形到游戏世界
    ApplyTerrainToWorld();
    
    // 记录地形管理器开始运行
    FWinstonLogger::Get().Info(TEXT("TerrainManager"), TEXT("Terrain manager started"));
}

void ATerrainManager::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    
    // 地形管理器通常不需要每帧更新
    // 可以在这里添加动态地形更新逻辑，如果需要的话
}

void ATerrainManager::GenerateNewTerrain(int32 Seed)
{
    // 如果没有指定种子，使用随机种子
    if (Seed == 0)
    {
        Seed = FMath::Rand();
    }
    
    CurrentTerrainSeed = Seed;
    
    // 记录地形生成开始
    FWinstonLogger::Get().Info(
        TEXT("TerrainManager"), 
        TEXT("Generating new terrain with seed: %d"),
        Seed
    );
    
    // 确保地形组件有效
    if (ProceduralTerrainComponent)
    {
        // 使用地形组件生成地形
        ProceduralTerrainComponent->GenerateProceduralTerrain(Seed);
        
        // 应用地形到世界
        ApplyTerrainToWorld();
        
        // 记录地形生成完成
        FWinstonLogger::Get().Info(
            TEXT("TerrainManager"), 
            TEXT("Terrain generation completed with seed: %d"),
            Seed
        );
    }
    else
    {
        FWinstonLogger::Get().Error(TEXT("TerrainManager"), TEXT("Procedural terrain component not initialized"));
    }
}

float ATerrainManager::GetHeightAtLocation(const FVector& WorldLocation)
{
    if (ProceduralTerrainComponent)
    {
        // 获取该位置的地形高度
        float Height = ProceduralTerrainComponent->GetHeightAt(WorldLocation.X, WorldLocation.Y);
        
        // 记录高度查询
        FWinstonLogger::Get().Debug(
            TEXT("TerrainManager"), 
            TEXT("Height query at (%f, %f): %f"),
            WorldLocation.X, WorldLocation.Y, Height
        );
        
        return Height;
    }
    
    // 如果地形组件无效，返回0高度
    FWinstonLogger::Get().Warning(
        TEXT("TerrainManager"), 
        TEXT("Height query failed at (%f, %f): component not initialized"),
        WorldLocation.X, WorldLocation.Y
    );
    
    return 0.0f;
}

void ATerrainManager::SetTerrainParameters(float NewNoiseScale, int32 NewOctaves, float NewPersistence, float NewLacunarity)
{
    // 确保地形组件有效
    if (ProceduralTerrainComponent)
    {
        // 在实际实现中，这里应该更新地形组件的参数
        // 然后重新生成地形
        GenerateNewTerrain(CurrentTerrainSeed);
        
        // 记录参数更新
        FWinstonLogger::Get().Info(
            TEXT("TerrainManager"), 
            TEXT("Terrain parameters updated - Scale: %f, Octaves: %d, Persistence: %f, Lacunarity: %f"),
            NewNoiseScale, NewOctaves, NewPersistence, NewLacunarity
        );
    }
}

void ATerrainManager::InitializeTerrainComponent()
{
    // 创建并初始化地形组件
    ProceduralTerrainComponent = CreateDefaultSubobject<UProceduralTerrain>(TEXT("ProceduralTerrain"));
    
    if (ProceduralTerrainComponent)
    {
        // 记录地形组件创建成功
        FWinstonLogger::Get().Debug(TEXT("TerrainManager"), TEXT("Procedural terrain component created"));
    }
    else
    {
        // 记录地形组件创建失败
        FWinstonLogger::Get().Error(TEXT("TerrainManager"), TEXT("Failed to create procedural terrain component"));
    }
}

void ATerrainManager::ApplyTerrainToWorld()
{
    // 在实际项目中，这里应该与UE5的Landscape组件交互
    // 或者生成地形网格并添加到世界中
    
    // 记录地形应用
    FWinstonLogger::Get().Debug(TEXT("TerrainManager"), TEXT("Applying terrain to world"));
    
    // 注意：这只是一个基础实现，实际的地形应用需要与UE5的地形系统集成
}